<template>
  <ContentField>
    <!-- <div style="display:flex;justify-content:center;align-items: center;text-align: center;">
      <h4>登录</h4>
    </div> -->
    <div class="rrrr" style="display:flex;justify-content:center;align-items: center;text-align: center;">
      <div class="activity">
        账号密码登录
      </div>
    </div>
    <div class="box">
      <el-form :model="form" ref="formRef" style="width: 50%;">
        <el-form-item>
          <el-input v-model="form.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="LoginByPwd">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </ContentField>
</template>

<script setup>
import ContentField from "@/components/ContentField.vue";
import { ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
import { useStore } from "vuex";
const store = useStore();

import { postJson } from '@/utils/api'

const formRef = ref(null);
const form = ref({
  username: '',
  password: '',
});

const LoginByPwd = async () => {
  try {
    let res = await postJson('/login', form.value)
    if (res.status == 200) {
      store.dispatch("login", res.data.data).then(() => {
        router.push({ name: 'index' });
      }).catch((error) => {
        console.log(error);
      });
    }
  } catch (error) {
    console.error('Login error:', error);
  }
};
</script>

<style scoped>
button {
  width: 100%;
}

div.error-message {
  color: red;
}

.rrrr {
  display: flex;
  flex-direction: row;
}

.activity {
  margin: 10px;
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
  /* border-bottom: 2px solid #333; */
  padding-bottom: 5px;
}

.normal {
  margin: 10px;
  cursor: pointer;
  font-size: 18px;
  font-weight: 600;
  color: #ccc;
  margin-bottom: 20px;
  padding-bottom: 5px;
}

div.box {
  width: 100%;
  display: flex;
  justify-content: center;
}

.inputbox {
  width: 60%;
}
</style>
